package com.wangs.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wangs.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @RequestMapping("/add")
    public String add() {
        return "hello 添加成功！";
    }

    @RequestMapping("/flow")
    @SentinelResource(value = "flow", blockHandler = "flowBlockHandler")
    public String flow() throws InterruptedException {
        return "正常访问";
    }

    public String flowBlockHandler(BlockException e) {
        return "流控";
    }

    @RequestMapping("/flowThread")
    @SentinelResource(value = "flowThread", blockHandler = "flowBlockHandler")
    public String flowThread() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        return "正常访问";
    }

    /**
     * 关联流控，
     * 访问/add 触发/get
     * 1秒内超过两次访问订单 触发查询流控
     *
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/get")
    public String get() throws InterruptedException {
        return "查询订单";
    }

    @RequestMapping("/test1")
    public String test1() {
        return orderService.getUser();
    }

    @RequestMapping("/test2")
    public String test2() {
        return orderService.getUser();
    }

    @RequestMapping("/err")
    public String err() {
        int a = 1 / 0;
        return "hello";
    }

}
